#include "loginuser.h"
#include "libs/base/utils/rc4.h"
#include "libs/base/utils/utils.h"
#include <QCryptographicHash>

LoginUser::LoginUser(const QJsonObject& jo)
{
    id_ = jo["id"].toInt();
    name_ = jo["name"].toString();
    const QString pwd = jo["key"].toString();
    password_ = QString::fromLatin1(
            xl::rc4::decrypt(pwd.toUtf8(), xl::baseutils::getDeviceId().toUtf8()));
    access_token_ = jo["accessToken"].toString();
    refresh_token_ = jo["refreshToken"].toString();
}

LoginUser::LoginUser(const QString& n, const QString& p) : id_(0), name_(n), password_(p) { }

LoginUser::LoginUser(int i, const QString& n, const QString& p, const QString& a, const QString& r)
    : id_(i), name_(n), password_(p), access_token_(a), refresh_token_(r)
{
}

QJsonObject LoginUser::toJson() const
{
    QJsonObject jo;
    jo["id"] = id_;
    jo["name"] = name_;
    jo["key"] = QString::fromUtf8(
            xl::rc4::crypt(password_.toUtf8(), xl::baseutils::getDeviceId().toUtf8()));
    jo["accessToken"] = access_token_;
    jo["refreshToken"] = refresh_token_;
    return jo;
}
